<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\ApiController;
use App\Http\Requests\Menu\AdminMenuRequest;
use App\Http\Resources\Menu\AdminMenuResource;
use App\Services\Menu\AdminMenuService;

/**
 * Class MenuController
 * @package App\Http\Controllers\Admin
 */
class MenuController extends ApiController
{

    /**
     * @var AdminMenuService
     */
    protected $adminMenuService;

    /**
     * MenuController constructor.
     * @param AdminMenuService $adminMenuService
     */
    public function __construct(AdminMenuService $adminMenuService)
    {
        $this->adminMenuService = $adminMenuService;
    }

    /**
     * @author Jerry
     * @return mixed
     */
    public function index()
    {
        $menus = $this->adminMenuService->getMenus();

        return $this->success($menus);
    }


    /**
     * @author Jerry
     * @param AdminMenuRequest $request
     * @return mixed
     */
    public function store(AdminMenuRequest $request)
    {
        $createData = [
            'title'         =>  $request->get('title'),
            'parent_id'     =>  $request->get('parent_id', 0),
            'order'         =>  $request->get('order', 0),
            'icon'          =>  empty($request->icon) ? '' : $request->icon,
            'method'        =>  (int) $request->get('method', 0),
            'route_name'    =>  empty($request->route_name) ? '' : $request->route_name,
            'route_match'   =>  empty($request->route_match) ? '' : $request->route_match,
            'remark'        =>  empty($request->remark) ? '' : $request->remark,
        ];

        try{
            $this->adminMenuService->insertMenu($createData);

            $menus = $this->adminMenuService->getMenus();

            return $this->success($menus);
        }catch (\Exception $exception) {
            return $this->failed('添加菜单失败', 500);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        try{
            $menu = $this->adminMenuService->findMenuById($id);

            return $this->success(new AdminMenuResource($menu));
        }catch (\Exception $exception) {
            return $this->notFond('不存在的菜单');
        }

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        try{
            $menu = $this->adminMenuService->findMenuById($id);

            return $this->success(new AdminMenuResource($menu));
        }catch (\Exception $exception) {
            return $this->notFond('不存在的菜单');
        }
    }

    /**
     * Update the specified resource in storage.
     * @author Jerry
     * @param AdminMenuRequest $request
     * @param $id
     * @return mixed
     */
    public function update(AdminMenuRequest $request, $id)
    {
        $data = [
            'title'         =>  $request->get('title'),
            'parent_id'     =>  $request->get('parent_id', 0),
            'order'         =>  $request->get('order', 0),
            'icon'          =>  empty($request->icon) ? '' : $request->icon,
            'method'        =>  (int) $request->get('method', 0),
            'route_name'    =>  empty($request->route_name) ? '' : $request->route_name,
            'route_match'   =>  empty($request->route_match) ? '' : $request->route_match,
            'remark'        =>  empty($request->remark) ? '' : $request->remark,
        ];

        try{
            $menu = $this->adminMenuService->updateMenuByMenuId($data, $id);

            return $this->success(new AdminMenuResource($menu));
        }catch (\Exception $exception) {
            return $this->failed('编辑失败', 500);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        try{
            $this->adminMenuService->deleteMenuByMenuId($id);

            return $this->success('删除成功');
        }catch (\Exception $exception) {
            return $this->failed('删除失败', 500);
        }
    }

    /**
     * 获取请求方法
     * @author Jerry
     * @return mixed
     */
    public function method()
    {
        return $this->success(AdminMenuService::$method);
    }
}
